package com.archetypus.projectschedule.core.projects.sf;

import javax.transaction.UserTransaction;

import com.archetypus.projectschedule.common.ServiceLocator;
import com.archetypus.projectschedule.common.ServiceLocatorException;
import com.archetypus.projectschedule.common.data.DAOFactory;
import com.archetypus.projectschedule.common.data.projects.ProjectDAO;
import com.archetypus.projectschedule.common.dto.projects.Project;
import com.archetypus.projectschedule.common.dto.projects.ProjectListHandler;
import com.archetypus.projectschedule.common.exceptions.ProjectSchedulerException;

public class ProjectSF implements IProjectSF {
	
	private static ProjectSF instance;
	
	protected ProjectSF(){}
	
	public static ProjectSF getInstance(){
		if(instance == null)
			instance = new ProjectSF();
		
		return instance;
	}
	
	
	@Override
	public void createProject(Project project) throws ProjectSchedulerException {
		UserTransaction userTransaction = null;
		
		try {
			userTransaction = ServiceLocator.getInstance().lookupUserTransaction();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
			throw new ProjectSchedulerException("Could not create transaction object",e);
		}
		
		try {
			userTransaction.begin();
			DAOFactory daoFactory = DAOFactory.getDAOFactory();
			ProjectDAO projectDAO = daoFactory.getProjectDAO();
			projectDAO.insert(project);
			userTransaction.commit();
		} catch (Exception e) {
			try{
				userTransaction.rollback();
			}catch(Exception ex){
				ex.printStackTrace();
				throw new ProjectSchedulerException("Could not rollback transaction",ex);
			}
			e.printStackTrace();
			throw new ProjectSchedulerException(e.getMessage(),e);
		}
	}

	@Override
	public void deleteProject(Project project) throws ProjectSchedulerException {
		UserTransaction userTransaction = null;
		
		try {
			userTransaction = ServiceLocator.getInstance().lookupUserTransaction();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
			throw new ProjectSchedulerException("Could not create transaction object",e);
		}
		
		try{
			userTransaction.begin();
			DAOFactory daoFactory = DAOFactory.getDAOFactory();
			ProjectDAO projectDAO = daoFactory.getProjectDAO();
			projectDAO.delete(project);
			userTransaction.commit();
		}catch(Exception e){
			try{
				userTransaction.rollback();
			}catch(Exception ex){
				ex.printStackTrace();
				throw new ProjectSchedulerException("Could not rollback transaction",ex);
			}
			e.printStackTrace();
			throw new ProjectSchedulerException(e.getMessage(),e);
		}

	}

	@Override
	public Project findProject(Project project) throws ProjectSchedulerException {
		UserTransaction userTransaction = null;
		Project retrievedProject = null;
		
		try {
			userTransaction = ServiceLocator.getInstance().lookupUserTransaction();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
			throw new ProjectSchedulerException("Could not create transaction object",e);
		}
		
		try{
			userTransaction.begin();
			DAOFactory daoFactory = DAOFactory.getDAOFactory();
			ProjectDAO projectDAO = daoFactory.getProjectDAO();
			retrievedProject = projectDAO.find(project);
			userTransaction.commit();
		}catch(Exception e){
			try{
				userTransaction.rollback();
			}catch(Exception ex){
				ex.printStackTrace();
				throw new ProjectSchedulerException("Could not rollback transaction",ex);
			}
			e.printStackTrace();
			throw new ProjectSchedulerException(e.getMessage(),e);
		}
		
		return retrievedProject;
	}

	@Override
	public ProjectListHandler listProjects() throws ProjectSchedulerException {
		UserTransaction userTransaction = null;
		ProjectListHandler listHandler = new ProjectListHandler();
		
		try {
			userTransaction = ServiceLocator.getInstance().lookupUserTransaction();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
			throw new ProjectSchedulerException("Could not create transaction object",e);
		}
		
		try{
			userTransaction.begin();
			DAOFactory daoFactory = DAOFactory.getDAOFactory();
			ProjectDAO projectDAO = daoFactory.getProjectDAO();
			listHandler.setList(projectDAO.list());
			userTransaction.commit();
		}catch(Exception e){
			try{
				userTransaction.rollback();
			}catch(Exception ex){
				ex.printStackTrace();
				throw new ProjectSchedulerException("Could not rollback transaction",ex);
			}
			e.printStackTrace();
			throw new ProjectSchedulerException(e.getMessage(),e);
		}
		
		return listHandler;
	}

	@Override
	public void updateProject(Project project) throws ProjectSchedulerException {
		UserTransaction userTransaction = null;
		
		try {
			userTransaction = ServiceLocator.getInstance().lookupUserTransaction();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
			throw new ProjectSchedulerException("Could not create transaction object",e);
		}
		
		try{
			userTransaction.begin();
			DAOFactory daoFactory = DAOFactory.getDAOFactory();
			ProjectDAO projectDAO = daoFactory.getProjectDAO();
			projectDAO.update(project);
			userTransaction.commit();
		}catch(Exception e){
			try{
				userTransaction.rollback();
			}catch(Exception ex){
				ex.printStackTrace();
				throw new ProjectSchedulerException("Could not rollback transaction",ex);
			}
			e.printStackTrace();
			throw new ProjectSchedulerException(e.getMessage(),e);
		}

	}

}
